<template>
  <div class="user-box" >
    <el-button type="primary" @click="handleAdd">新增员工</el-button>

    <el-table :data="users" @selection-change="selectChange" style="width: 100%">
      <el-table-column prop="id" label="员工id" width="180">
      </el-table-column>
      <el-table-column prop="name" label="姓名" width="180">
      </el-table-column>
      <el-table-column prop="tel" label="手机" width="180">
      </el-table-column>
      <el-table-column prop="sex" label="性别" width="180">
      </el-table-column>
      <el-table-column prop="address" label="地址">
      </el-table-column>
      <el-table-column prop="salary" label="薪资" width="180">
      </el-table-column>
      <el-table-column label="操作" fixed="right" width="150">
        <template slot-scope="scope">
          <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog :title="dialogTitle" width="600px" :visible.sync="userFormVisible" @close="resetForm('userForm')">
      <el-form :model="user" :rules="rules" ref="userForm">
        <el-form-item label="姓名" prop="name" label-width="50px">
          <el-input v-model="user.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="性别" prop="sex" label-width="50px">
          <el-input v-model="user.sex" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="电话" prop="tel" label-width="50px">
          <el-input v-model="user.tel" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="地址" prop="address" label-width="50px">
          <el-input v-model="user.address" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="薪资" prop="salary" label-width="50px">
          <el-input v-model="user.salary" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="userFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitUser('userForm')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import * as requires from '../network/require'

  export default {
    data() {
      return {
        users: [
          {
            address: 'address',
            id: 1,
            name: 'name',
            salary: 1000,
            sex: '男',
            tel: '110'
          }
        ],
        user: {
          address: '',
          name: '',
          salary: '',
          sex: '',
          tel: ''
        },
        userBackup: Object.assign({}, this.user),
        multipleSelection: [],
        userFormVisible: false,
        dialogTitle: '',
        rowIndex: 9999,
        rules: {
          name: [
            {required: true, message: '请输入姓名', trigger: 'blur'},
            {min: 2, max: 5, message: '长度在 2 到 5 个字符', trigger: 'blur'}
          ],
          sex: [
            {required: true, message: '请输入性别', trigger: 'blur'}
          ],
          tel: [
            {required: true, message: '请输入电话', trigger: 'blur'}
          ],
          address: [
            {required: true, message: '请输入地址', trigger: 'blur'}
          ],
          salary: [
            {required: true, message: '请输入薪资', trigger: 'blur'}
          ]
        }
      }
    },
    mounted() {
      this.getUsers()
    },
    methods: {
      getUsers() {
        this.loading = true
        requires.getWorker().then((res) => {
          this.users = res.data
        }).catch((err) => {
          console.error(err)
        })
      },
      handleEdit(index, row) {
        this.dialogTitle = '编辑'
        this.user = Object.assign({}, row)
        this.userFormVisible = true
        this.rowIndex = index
      },
      submitUser(formName) {
        // 表单验证
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.userFormVisible = false
            requires.addWorker(this.user).then((res) => {
              console.log(res);
              this.$message({
                type: 'success',
                message: `新增成功,此员工员工号为${res.data}！`
              })
              this.user.id = res.data;
              this.users.unshift(this.user);
            });

          }
        })
      },
      handleDelete(index, row) {
        this.$confirm(`确定删除员工 【${row.name}】 吗?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          requires.deleteWorker({id: row.id}).then((res) => {
            this.users.splice(index, 1)
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
          })
        }).catch(() => {
          console.log('取消删除')
        })
      },
      resetForm(formName) {
        this.$refs[formName].clearValidate()
      },
      selectChange(val) {
        this.multipleSelection = val
      },
      handleAdd() {
        this.dialogTitle = '新增'
        this.user = {
          address: '',
          name: '',
          salary: '',
          sex: '',
          tel: ''
        };
        this.userFormVisible = true
      }
    }
  }
</script>

<style lang="scss" scoped>
  .user-box {
    width: 100%;

    .tool-box {
      padding: 10px 10px;
      border-bottom: 1px solid #eee;
    }

    .el-pagination {
      margin-top: 20px;
    }
  }
</style>
